# coding:utf-8
# http://sklearn.lzjqsdd.com/modules/preprocessing.html
#Standardization标准化:将特征数据的分布调整成标准正太分布，也叫高斯分布，也就是使得数据的均值维0，方差为1.
#它的调整是对每一列进行调整，让每一列的方差为1，均值为0，在sklearn.preprocessing中提供了一个scale的方法，可以实现以上功能
from sklearn import preprocessing
import numpy as np
x = np.array([[1., -1., 2.],
              [2., 0., 0.],
              [0., 1., -1.]])
x_scale = preprocessing.scale(x)
print(x_scale)
print(x_scale.mean(axis=0))
print(x_scale.std(axis=0))

#X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
#X_scaled = X_std / (max - min) + min
# 标准化 [0,1]区间
min_max_scaler = preprocessing.MinMaxScaler()
x_minmax = min_max_scaler.fit_transform(x)
# print(x_minmax)

#MaxAbsScaler
max_abs_scale = preprocessing.MaxAbsScaler()
x_train_maxabs = max_abs_scale.fit_transform(x)
# print(x_train_maxabs)

# 正则化
x_noamal1 = preprocessing.normalize(x,norm='l1')
# print(x_noamal1)
x_noamal2 = preprocessing.normalize(x,norm='l2')
# print(x_noamal2)
x_naomal = preprocessing.Normalizer()
res = x_naomal.fit_transform(x)
# print(res)

# 二值化
binarizer = preprocessing.Binarizer()
res = binarizer.fit_transform(x)
# print(res)

# 独热编码
labels = [0,1,0,2]
# 行向量转列向量
labels = np.array(labels).reshape(len(labels), -1)
enc = preprocessing.OneHotEncoder()
enc.fit(labels)
targets = enc.transform(labels).toarray()
#标签量化
enc = preprocessing.OneHotEncoder()
enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])
# print(enc.n_values_)#每个特征对应的最大位数,即每个特征需要几位的码位，这个总共需要9位
# print(enc.transform([[0,1,3]]).toarray())
# print(enc.transform([[0,1,1]]).toarray())

# 缺失值处理
from sklearn.preprocessing import Imputer
imp = Imputer(missing_values='NaN', strategy='mean', axis=0)  # 使用特征的均值进行填充，其余还有使用众数填充等,只需要把mean改成median即可
data = np.array([np.nan, 2, 6, np.nan, 7, 6]).reshape(3,2)
 
print(data)
print(imp.fit_transform(data))


##############################
import pandas as pd
# 分箱离散 面元划分
# pd.cut()